#include "Renderer_PCH.h"
#include "TextureRequest.h"

/*--------------------------------------------------------------------------*/

TextureRequest::TextureRequest(const std::string& name, int width, int height, const std::string& format) :
	m_name(name),
	m_width(width),
	m_height(height),
	m_format(format)
{

}

/*--------------------------------------------------------------------------*/

TextureRequest::TextureRequest( const std::string& path ) :
	m_name(path),
	m_width(SIZE_DOES_NOT_MATTERS),
	m_height(SIZE_DOES_NOT_MATTERS),
	m_format("SOIL")
{
	
}

/*--------------------------------------------------------------------------*/

TextureRequest::~TextureRequest()
{

}

/*--------------------------------------------------------------------------*/

void* TextureRequest::DoRequest()
{
	if (m_width != SIZE_DOES_NOT_MATTERS && m_height != SIZE_DOES_NOT_MATTERS)
	{
		RTexture* texture = g_Renderer->GetRendererStorageMgr()->CreateTexture(m_name);
		__Check(texture);

		glGenTextures(1, &texture->m_handle);
		__Check(texture->m_handle);

		glBindTexture(GL_TEXTURE_2D, texture->m_handle);

		uint width = m_width;
		uint height = m_height;

		if (!width || !height)
		{
			width	= g_Renderer->GetResX();
			height	= g_Renderer->GetResY();
		}

		GLenum format = GL_RGBA8;

		if (strcmp(m_format.c_str(), "RGBA8") == 0)
		{
			format = GL_RGBA8;
		}
		else if (strcmp(m_format.c_str(), "D24S8") == 0)
		{
			format = GL_DEPTH24_STENCIL8;
		}
		else
		{
			__Check(false);
		}

		texture->m_width = width;
		texture->m_height = height;

		glTexStorage2D(GL_TEXTURE_2D, 1, format, width, height);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		__Info("[RENDERER] Texture %s created as %d", texture->m_name.c_str(), texture->m_handle);
		return texture;
	}
	else
	{
		RTexture* texture = g_Renderer->GetRendererStorageMgr()->CreateTexture(::GetFileName(m_name));
		__Check(texture);

		glGenTextures(1, &texture->m_handle);
		__Check(texture->m_handle);

		texture->m_handle = SOIL_load_OGL_texture(	m_name.c_str(),
													SOIL_LOAD_AUTO,
													texture->m_handle,
													SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
													);
	
		__Check(texture->m_handle);
	
		glBindTexture(GL_TEXTURE_2D, texture->m_handle);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (int*) &texture->m_width);
		glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (int*) &texture->m_height);

		__Info("[RENDERER] Texture %s loaded as %d", texture->m_name.c_str(), texture->m_handle);
		return texture;
	}
}

/*--------------------------------------------------------------------------*/
